package demo;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

  @RequestMapping("hello")
  public String hello() {
    return "HelloWorld";
  }

  @Autowired
  private UserService userService;

  @RequestMapping("user/getAll")
  public JSONObject getAll() {
    JSONObject result = new JSONObject();
    JSONObject data = new JSONObject();
    data.put("total", 1);
    data.put("rows", userService.getAll());
    result.put("code", 200);
    result.put("data", data);
    return result;
  }

  /**
   * 根据ID删除某个用户记录
   *
   * @return
   */
  @RequestMapping(value = "user/delete/{id}", method = RequestMethod.GET)
  public String delete(@PathVariable Integer id) {
    System.out.println("id is " + id);
    return userService.delete(id) ? "刪除成功" : "刪除失敗";
  }

  /**
   * 添加用户接口
   *
   * @return
   */
  @RequestMapping("user/add")
  public List<User> add() {
    System.out.println("11");
    return userService.getAll();
  }

  /**
   * 用戶登录接口
   *
   * @return
   */
  @RequestMapping("user/login")
  public JSONObject login() {
    JSONObject jsonObject = new JSONObject();
    JSONObject data = new JSONObject();
    data.put("userId", "defalut");
    data.put("nickname", "admin");
    data.put("username", "fuckyou");
    jsonObject.put("code", 200);
    jsonObject.put("data", data);
    return jsonObject;
  }
}